package com.allen.study.domain.service;

import com.allen.study.common.base.DomainResponse;
import com.allen.study.domain.entity.ApprovalRecord;
import com.allen.study.domain.repository.IApprovalRecordRepo;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import javax.validation.constraints.NotNull;

/**
 * 审批记录表领域服务
 *
 * @author AllenSun
 * @since 2025-04-23 20:46
 */
@Service
@AllArgsConstructor
public class ApprovalRecordService {

    /**
     * 审批记录表资源库
     */
    private final IApprovalRecordRepo approvalRecordRepo;

    /**
     * 创建审批记录表
     *
     * @param approvalRecord 审批记录表实体
     * @return 响应结果
     */
    public DomainResponse<Void> create(@NotNull ApprovalRecord approvalRecord) {
        // 获取当前登录用户
        // SecurityUser securityUser = SecurityUserHelper.get();
        // 设置租户
        // approvalRecord.setTenantId(securityUser.getTenantId());

        // 保存审批记录表
        approvalRecordRepo.create(approvalRecord);
        return DomainResponse.ok();
    }

    /**
     * 根据主键删除审批记录表
     *
     * @param approvalRecordId 审批记录表主键
     * @return 响应结果
     */
    public DomainResponse<Void> deleteById(@NotNull Long approvalRecordId) {
        // 根据主键查询审批记录表
        ApprovalRecord approvalRecord = approvalRecordRepo.queryById(approvalRecordId);
        // 审批记录表数据存在且租户正确，删除审批记录表数据
        if (ObjectUtils.isNotEmpty(approvalRecord) && approvalRecord.checkTenant()) {
            approvalRecordRepo.deleteById(approvalRecordId);
        }
        return DomainResponse.ok();
    }

    /**
     * 根据主键更新审批记录表
     *
     * @param approvalRecord 审批记录表实体
     * @return 响应结果
     */
    public DomainResponse<Void> updateById(@NotNull ApprovalRecord approvalRecord) {
        // 根据主键查询审批记录表
        ApprovalRecord exists = approvalRecordRepo.queryById(approvalRecord.getId());
        // 审批记录表数据存在且租户正确，更新审批记录表数据
        if (ObjectUtils.isNotEmpty(exists) && exists.checkTenant()) {
            approvalRecordRepo.updateById(approvalRecord);
        }
        return DomainResponse.ok();
    }

    /**
     * 根据主键查询审批记录表
     *
     * @param approvalRecordId 审批记录表主键
     * @return 响应结果
     */
    public DomainResponse<ApprovalRecord> queryById(@NotNull Long approvalRecordId) {
        ApprovalRecord approvalRecord = approvalRecordRepo.queryById(approvalRecordId);
        return DomainResponse.ok(approvalRecord);
    }
}
